package com.bms.cfcmms.controller.system;

import com.bms.cfcmms.controller.BaseController;
import com.bms.cfcmms.model.system.User;
import com.bms.cfcmms.service.UserService;
import com.bms.cfcmms.util.ActionResult;
import com.bms.cfcmms.util.SpringUtils;
import com.bms.cfcmms.util.annotation.Authorization;
import com.bms.cfcmms.util.annotation.OperationLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by carl on 2016/4/9.
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    final Logger log = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    /**
     * 重置密码的函数入口.
     * <p>
     * 根据用户记录在数据库中的主键来更新重置密码
     * </p>
     *
     * @param userId 需要重置的用户的在数据库中的主键信息
     * @return 返回一个ActionResult对象
     */
    @Authorization
    @RequestMapping("/resetpassword.do")
    public ActionResult resetPassword(Integer userId) {
        SpringUtils.getMessage("module.notfind");
        log.info("进入到resetpassword.do的controller中");
        ActionResult result = new ActionResult();
        try {
            userService.resetPassword(userId);
            result.setSuccess(true);
        } catch (Exception e) {
            result.setSuccess(false);
            log.error("", e);
        }
        return result;
    }

    @Authorization
    @OperationLog(description = "add.user.title")
    @RequestMapping("/addUser")
    public ActionResult addSaveToDb(@ModelAttribute @Validated User user, Errors errors) {
        ActionResult actionResult = new ActionResult();
        if (errors.hasErrors()) {
            actionResult.setData(errors.getAllErrors());
            actionResult.setMsg(this.getOperDescInfo());
            actionResult.setSuccess(false);
            return actionResult;
        }
        int id = userService.saveUserIntoDb(user);

        setOperDescInfo(user);
        actionResult.setData(id == 0 ? user.getLoginName() : id);
        actionResult.setMsg(this.getOperDescInfo());
        actionResult.setSuccess(id == 0 ? false : true);
        return actionResult;
    }

    @Authorization
    @OperationLog(description = "query.users.title")
    @RequestMapping("/queryuser")
    public ActionResult queryAllUsers() {
        ActionResult actionResult = new ActionResult();
        List<User> users = userService.queryAllUsers();
        actionResult.setData(users);
        actionResult.setSuccess(true);

        String operDescInfo = SpringUtils.getMessage("querys.all.users.desc");
        this.setOperDescInfo(operDescInfo);
        actionResult.setMsg(operDescInfo);
        return actionResult;
    }
}
